/*
 *  Copyright 2004 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.webapp.action.core.grid;

import com.blandware.atleap.webapp.form.core.StringFilterForm;
import com.blandware.atleap.webapp.taglib.core.grid.util.Grid;
import com.blandware.atleap.webapp.taglib.core.grid.util.StringFilter;
import com.blandware.atleap.webapp.util.core.WebappUtil;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;

/**
 * <p>Calls string filter JSP and sets form bean properties according to filter stored in session
 * </p>
 * <p><a href="CallStringFilterAction.java.html"><i>View Source</i></a></p>
 * <p/>
 *
 * @author Sergey Zubtsovskiy <a href="mailto:sergey.zubtsovskiy@blandware.com">&lt;sergey.zubtsovskiy@blandware.com&gt;</a>
 * @version $Revision: 1.10 $ $Date: 2008/05/23 08:09:15 $
 * @struts.action path="/grid/callStringFilter"
 * name="stringFilterForm"
 * scope="request"
 * @struts.action-forward name="showFilterPage"
 * path=".stringFilter"
 * @struts.action-forward name="filterError"
 * path=".filterError"
 */
public final class CallStringFilterAction extends BaseGridAction {

	/**
	 * @param mapping  The ActionMapping used to select this instance
	 * @param form     The optional ActionForm bean for this request (if any)
	 * @param request  The HTTP request we are proceeding
	 * @param response The HTTP response we are creating
	 * @return an ActionForward instance describing where and how
	 *         control should be forwarded, or null if response
	 *         has already been completed
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
	                             HttpServletRequest request, HttpServletResponse response) throws JspException {

		HttpSession session = request.getSession();

		StringFilterForm stringFilterForm = (StringFilterForm) form;
		String fieldName = stringFilterForm.getFieldName();
		String gridName = stringFilterForm.getGridName();

		Grid grid = getGridByName(gridName, session);

		if ( grid == null ) {
			if ( log.isWarnEnabled() ) {
				String errorMessage = "No grid with name " + gridName + " could be found in session";
				log.warn(errorMessage);
			}
			return mapping.findForward("filterError");
		}

		StringFilter filter = null;
		try {
			filter = (StringFilter) grid.getFilterByFieldName(fieldName);
		} catch ( ClassCastException e ) {
			if ( log.isErrorEnabled() ) {
				String errorMessage = "Filter for field \'" + fieldName + "\' in grid \'" + gridName + "\' is not instance of com.blandware.atleap.webapp.taglib.core.grid.util.StringFilter";
				log.error(errorMessage);
			}
			return mapping.findForward("filterError");
		}

		if ( filter != null ) {
			try {
				String rowIterators = stringFilterForm.getRowIterators();
				WebappUtil.copyProperties(stringFilterForm, filter, request);
				stringFilterForm.setRowIterators(rowIterators);
			} catch ( Exception e ) {
				if ( log.isErrorEnabled() ) {
					log.error(e);
				}
				throw new JspException(e);
			}
		}

		if ( log.isDebugEnabled() ) {
			log.debug("Returning filter page: " + mapping.findForward("showFilterPage").getPath());
		}

		return mapping.findForward("showFilterPage");
	}
}